var isAdd = false;
var bankCarGrid, bankCarStore, bankCarDialog;
$(function () {
    if (user.identityCard) {
        $(".userNameLabel").html(user.userName)
        $(".identityCardLabel").html(user.identityCard)
        $(".identityCard").hide()
        $(".userName").hide()
        $(".userBtn").hide()
    } else {
        $(".userNameLabel").hide()
        $(".identityCardLabel").hide()
        $(".identityCard").show()
        $(".userName").show()
        $(".userBtn").show()
    }
    initGrid();
    initDialog();
    initEvent()
});

function initEvent() {
    $(".userBtn").on("click", function () {
        if (!userEditForm.isValid()) {
            return;
        }
        var formVal = userEditForm.serializeToObject();
        CommonClass.AjaxAction(ctx + "/user/user/saveUser", formVal, function (data) {
            if (data.success) {
                Message.show(data.msg, "success", 2000);
                $(".userNameLabel").html(formVal.userName)
                $(".identityCardLabel").html(formVal.identityCard)
                $(".userNameLabel").show()
                $(".identityCardLabel").show()
                $(".identityCard").hide()
                $(".userName").hide()
                $(".userBtn").hide()
            } else {
                Message.show(data.msg, "error", 2000);
            }
        });

    });
}

function initGrid() {

    var Grid = BUI.Grid,
        Store = BUI.Data.Store,
        Format = Grid.Format,
        columns = [
            {title: 'id', dataIndex: 'id', width: 100, visible: false},
            {title: 'userId', dataIndex: 'userId', width: 100, visible: false},
            {title: '银行名称', dataIndex: 'bankName', width: 100},
            {title: '卡号', dataIndex: 'carNo', width: 180}
        ];

    bankCarStore = new Store({
        url: ctx + "/user/user/getBankInfo",
        autoLoad: true,
        root: 'list',               //存放数据的字段名(rows)
        totalProperty: 'total',
        pageSize: 1000000	// 配置分页数目
    });
    bankCarGrid = new Grid.Grid({
        render: '#bankCarGrid',
        columns: columns,
        loadMask: true,
        forceFit: true,
        store: bankCarStore,
        plugins: [Grid.Plugins.CheckSelection, Grid.Plugins.AutoFit], //勾选插件、自适应宽度插件
        tbar: {
            items: [{
                btnCls: 'button button-success button-size',
                text: '新增',
                handler: addbankCar
            }, {
                btnCls: 'button button-success button-size',
                text: '修改',
                handler: editbankCar
            }, {
                btnCls: 'button button-success button-size',
                text: '删除',
                handler: deletebankCar
            }]
        }
    });
    bankCarGrid.render();
    //创建表单，表单中的日历，不需要单独初始化

    BUI.use(['bui/grid', 'bui/data', 'bui/form'], function (Grid, Data, Form) {
        var form = new Form.HForm({
            srcNode: '#bankCarSearchForm'
        });
        form.on('beforesubmit', function (ev) {
            //序列化成对象
            var obj = form.serializeToObject();
            obj.start = 0; //返回第一页
            bankCarStore.load(obj);
            return false;
        });
        form.render();
    })

    bankCarStore.load();
}

function initDialog() {

    bankCarEditForm = Bui.formRender("bankCarEditForm");
    userEditForm = Bui.formRender("userEditForm");
    BUI.use('bui/overlay', function (Overlay) {
        bankCarDialog = new Overlay.Dialog({
            id: 'bankCarEditFormDialog',
            title: '修改银行卡信息',
            width: 400,
            height: 200,
            //配置DOM容器的编号
            contentId: 'bankCarEditFormDialogContent',
            buttons: [
                {
                    text: '保存',
                    elCls: 'button button-primary button-size',
                    handler: function () {
                        if (!bankCarEditForm.isValid()) {
                            return;
                        }
                        var formVal = bankCarEditForm.serializeToObject();

                        if (isAdd) {
                            formVal.is = null;
                        }
                        formAction(formVal, ctx + "/user/user/saveBankInfo")

                    }
                }, {
                    text: '关闭',
                    elCls: 'button button-size',
                    handler: function () {
                        this.close();
                    }
                }
            ]
        });
    });
}

function addbankCar() {
    $('.rechargeTime').attr("disabled", false);
    $('.userName').attr("disabled", false);
    isAdd = true;
    bankCarDialog.set("title", "新增银行卡信息");
    document.getElementById("bankCarEditForm").reset();
    CommonClass.fieldVaild();
    bankCarDialog.show();
}

function editbankCar() {
    var selections = bankCarGrid.getSelection();
    isAdd = false
    if (selections.length == 0) {
        BUI.Message.Alert('请选择一条记录！', function () {
        }, 'info');
        return;
    }
    if (selections.length > 1) {
        BUI.Message.Alert('只能选择一条记录！', function () {
        }, 'info');
        return;
    }
    document.getElementById("bankCarEditForm").reset();
    bankCarEditForm.setRecord(selections[0]);

    bankCarDialog.set("title", "修改银行卡信息");
    bankCarDialog.show();


}

function deletebankCar() {
    CommonClass.deleteGridData(bankCarGrid, bankCarStore, ctx + "/user/user/deleteBankInfo");
}

function formAction(formVal, actionUrl) {
    CommonClass.AjaxAction(actionUrl, formVal, function (data) {
        if (data.success) {
            Message.show(data.msg, "success", 2000);
            bankCarStore.load()
            bankCarDialog.close();
        } else {
            Message.show(data.msg, "error", 2000);
        }
    });
}